/**
 * Copyright [2010] Delta Air Lines, Inc. All Rights Reserved Access,
 * Modification, or Use Prohibited Without Express Permission of Delta Air
 * Lines.
 */
package com.delta.rafg.core.componentbase.exception;


import java.io.Serializable;

import com.delta.rafg.core.log.LogData;
import com.delta.rafg.core.log.RAFGLogger;
import com.delta.rafg.coredata.AObject;


/**
 * @author RAFG Development Team (D9785692)
 */
public class DLException extends Exception {

    /**
     * serialVersionUID property.
     */
    private static final long serialVersionUID = 6203490043397039551L;

    /**
     * originalException property.
     */
    private Throwable originalException;

    /**
     * objHelper property.
     */
    private AObject objHelper;

    /**
     * logData property.
     */
    private LogData logData = null;

    /**
     * @param message message.
     */
    public DLException(final String message) {

        this(message, null, null);
    }

    /**
     * DLException Constructor.
     * @param message message
     * @param pRequest request
     * @param pResponse response
     * @param pOtherObjects otherobjects
     */
    public DLException(final String message, final Serializable pRequest,
        final Serializable pResponse, final Object... pOtherObjects) {

        super(message);
        logIt(message, null, pRequest, pResponse, pOtherObjects);
    }

    /**
     * DLException Constructor.
     * @param message message
     * @param cause cause
     */
    public DLException(final String message, final Throwable cause) {

        this(message, cause, (Serializable) null, (Serializable) null);
    }

    /**
     * DLException Constructor.
     * @param message message
     * @param cause cause
     * @param pRequest request
     * @param pResponse response
     * @param pOtherObjects otherObjects
     */
    public DLException(final String message, final Throwable cause,
        final Serializable pRequest, final Serializable pResponse,
        final Object... pOtherObjects) {

        super(message, cause);
        originalException = cause;
        logIt(message, cause, pRequest, pResponse, pOtherObjects);
    }

    /**
     * DLException Constructor.
     * @param cause cause
     */
    public DLException(final Throwable cause) {

        this(cause, null, null);
    }

    /**
     * DLException Constructor.
     * @param cause cause
     * @param pRequest request
     * @param pResponse response
     * @param pOtherObjects otherobjects
     */
    public DLException(final Throwable cause, final Serializable pRequest,
        final Serializable pResponse, final Object... pOtherObjects) {

        super(cause);
        originalException = cause;
        logIt(cause.getMessage(), cause, pRequest, pResponse, pOtherObjects);
    }

    /**
     * @return the logData.
     */
    public final LogData getLogData() {

        return logData;
    }

    /**
     * Returns the original Exception with which this Exception was created.
     * Usually passed via the constructor.
     * @return orginalException
     */
    public final Throwable getOriginalException() {

        return originalException;
    }

    /**
     * @param pFreeText freetest
     * @param cause cause
     * @param pRequest request
     * @param pResponse response
     * @param pOtherObjects objects
     */
    private void logIt(final String pFreeText, final Throwable cause,
        final Serializable pRequest, final Serializable pResponse,
        final Object... pOtherObjects) {

        logData =
            RAFGLogger.error(pFreeText, cause, pRequest, pResponse,
                pOtherObjects);
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Throwable#toString()
     */
    @Override
    public final String toString() {

        if (objHelper == null) {
            objHelper = new AObject() {

                private static final long serialVersionUID = 1L;
            };
        }
        return objHelper.toXML(this);
    }
}
